package com.wxl.www.wxl.shiro.config;


import com.wxl.www.wxl.shiro.entity.SysPermission;
import com.wxl.www.wxl.shiro.entity.SysRole;
import com.wxl.www.wxl.shiro.entity.UserInfo;
import com.wxl.www.wxl.shiro.service.SysRoleService;
import com.wxl.www.wxl.shiro.service.UserInfoService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈自定义Realm〉
 *
 * @author X450J
 * @create 2019/5/30
 * @since 1.0.0
 */
public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SysRoleService sysRoleService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        UserInfo userName = (UserInfo) principalCollection.getPrimaryPrincipal();
        UserInfo user = userInfoService.findUserByName(userName.getUserName());
        SysRole sysRole=sysRoleService.findByUserInfo(user);
//        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        for (SysRole role : user.getRoles()) {
        List<String> permissionsString = new ArrayList<String>();
            //添加角色
            simpleAuthorizationInfo.addRole(sysRole.getRole());
            //添加权限
            for (SysPermission permissions : sysRole.getPermissions()) {
                simpleAuthorizationInfo.addStringPermission(permissions.getPerms());
            }

//        }
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        //进行用户信息获取
        String username = (String)usernamePasswordToken.getUsername();
        String password = new String(usernamePasswordToken.getPassword());
        UserInfo user = userInfoService.findUserByName(username);
       String mdPassword= this.md5(username,password);
        if(user!=null&&mdPassword.equals(user.getPassWord())&&user.getUserName().equals(username)){

            return  new SimpleAuthenticationInfo(user, user.getPassWord(), ByteSource.Util.bytes(user.getUserName()), getName());
        }else{
            throw new UnknownAccountException();
        }

    }

    //生成一个加盐密码
    public static String  md5(String username,String password){
        //加密类型
        String hashAlgorithmName = "md5";
        //迭代次数
        Integer count = 2;
        String s = new SimpleHash(hashAlgorithmName,password,username,count).toHex();
        return s;

    }

    public static void main(String[] args) {
        //加密类型
        String hashAlgorithmName = "md5";
        //迭代次数
        Integer count = 2;
        String s = new SimpleHash(hashAlgorithmName,"123456","admin",count).toHex();
        System.out.println(s);
    }
}